<?php

/** Zend_Controller_Action */
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';

class MenuController extends Soooner_Controller_Action
{
	private $_roleid;
	private $_modelMenu;

    public function init()
    {
        /* Initialize action controller here */
        $this->initDzTemplate();
    }

    public function delajaxAction()
    {
        /* Initialize action controller here */
        $menuid = intval($this->_getParam('menuid'));
        if (empty($menuid))
        {
            exit;
        }
        $this->_modelMenu = new Model_Tables_Menu();
        $this->_modelMenu->deleteMenu($menuid);
        $this->_modelMenu->update_isfolder();
        echo 'd'.$menuid;exit;
    }

    public function saveajaxAction()
    {
        /* Initialize action controller here */
    	$type = $this->_getParam('type');
    	if (empty($type))
    	{
    		exit;
    	}
    	if ('name'==$type)
    	{
	        $request = $this->getRequest();
	        $params = $request->getPost();
	        $params['id'] = trim($params['id']);
	        if (empty($params['id']))
	        {
	        	exit;
	        }
	        $params['id'] = substr($params['id'], 1);
	        $menuid = intval($params['id']);
	        $menuname = stripslashes($params['value']);
	        $this->_modelMenu = new Model_Tables_Menu();
	        $this->_modelMenu->updateMenuName($menuid, $menuname);
	        echo $menuname;exit;
    	}
        else if ('order'==$type)
        {
            $request = $this->getRequest();
            $params = $request->getPost();
            $params['id'] = trim($params['id']);
            if (empty($params['id']))
            {
                exit;
            }
            $params['id'] = substr($params['id'], 1);
            $menuid = intval($params['id']);
            $menuorder = intval($params['value']);
            $menuorder = (0>$menuorder) ? 0 : $menuorder;
            $this->_modelMenu = new Model_Tables_Menu();
            $this->_modelMenu->updateMenuOrder($menuid, $menuorder);
            echo $menuorder;exit;
        }
//        else if ('delete'==$type)
//        {
//            $menuid = intval($this->_getParam('menuid'));
//            if (empty($menuid))
//            {
//                exit;
//            }
//            $this->_modelMenu = new Model_Tables_Menu();
//            $this->_modelMenu->deleteMenu($menuid);
//            $this->_modelMenu->update_isfolder();
//            echo 'd'.$menuid;exit;
//        }
        else if ('getchild'==$type)
        {
	        $filter = "3,2,6,7";//主3,2,6,7从1,3,5,7代理4,5,6,7
	    	if($this->_configs["state"]=="slave")$filter = "1,3，5,7";
	    	if($this->_configs["state"]=="master"){
	    		if($this->_configs["isproxy"]){//代理
	    			$filter = "4,5,6,7";
	    		}else{
	    			$filter = "3,2,6,7";
	    		}
	    	}
	    	
        	$parentid = intval($this->_getParam('parentid'));
            $this->_modelMenu = new Model_Tables_Menu();
            $result = $this->_modelMenu->getChild($parentid,$filter);
            include $this->_dzTemplate->getFile( 'ajax/menu.getchild.ajax.html' );
        }
    	exit;
    }

    public function indexajaxAction()
    {
//    	$a = array('第一页','上一页','下一页','最末页');
//    	foreach($a as $i){echo $i . ':'. json_encode($i) . '<br>';};exit;
        $numPerPage = 10;//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;

        $parentid = intval($this->_getParam('parentid'));
        //if (!$parentid) $parentid = 2;  //2：常用菜单
        $this->_modelMenu = new Model_Tables_Menu();
        $result = $this->_modelMenu->getPartMenuObj( $parentid, $page, $numPerPage);
        include $this->_dzTemplate->getFile( 'ajax/menu.index.ajax.html' );
    }

    public function indexAction()
    {
        // action body
        $numPerPage = 10;//每页显示的条数
        $page = intval($this->_getParam('page'));//默认页码
        $page = empty($page)?'1':$page;
        $parentid = intval($this->_getParam('parentid'));
        $parentname = $this->_getParam('parentname');
        //if (!$parentid) $parentid = 2;  //2：常用菜单
        $this->_modelMenu = new Model_Tables_Menu();
        if ($parentid)
        {
            $result = $this->_modelMenu->get($parentid);
            $parentname = $result['name'];
            $parentname = urlencode($parentname);
        }
        $ItemCount = $this->_modelMenu->getMenuCount( $parentid );
        $PageCount = ceil($ItemCount/$numPerPage);
        $PageCount = $PageCount ? $PageCount : 1;
        include $this->_dzTemplate->getFile( 'menu.index.html' );
    }

    public function editAction()
    {
        // action body

        $request = $this->getRequest();
    	if ($request->isPost())
    	{
            $params = $request->getPost();
            $menuid = intval($params['menuid']);
            if (!$menuid)
            {
            	throw new Exception('缺少参数');
            }
            if (!Zend_Validate::is($params['info']['name'], 'StringLength', array(2, 20)))
            {
                throw new Exception('菜单名称只能为 2-20 个字符');
            }
            $params['info']['name'] = stripslashes($params['info']['name']);
            $params['info']['url'] = stripslashes($params['info']['url']);
            $params['info']['css'] = stripslashes($params['info']['css']);
            $params['info']['js'] = stripslashes($params['info']['js']);

//            if (empty($params['roleids'])||'-99'==$params['roleids'])
//            {
//                $params['info']['roleids'] = '';
//            }
//            else
//            {
//            	$params['info']['roleids'] = implode(',', $params['roleids']);
//            }
            if (empty($params['resourceid']))
            {
                $params['info']['resourceid'] = '0';
            }
            else
            {
                $params['info']['resourceid'] = $params['resourceid'];
            }

            $this->_modelMenu = new Model_Tables_Menu();
            $this->_modelMenu->updateMenu($menuid, $params['info']);
            $params['forward'] = urldecode($params['forward']);
            $forward = empty($params['forward']) ? '/menu/index/' : $params['forward'];
            $this->_redirect($forward);
    	}
        $menuid = intval($this->_getParam('menuid'));
        if (!$menuid)
        {
        	throw new Exception('无效ID。');
        }

        //读取角色名单
        $modelRole = new Model_Tables_Role();
        $RoleNameList = $modelRole->getNameList();
        $RoleName = array();
        if (!empty($RoleNameList))
        {
            foreach ($RoleNameList as $item)
            {
                $RoleName[$item['roleid']] = $item['name'];
            }
        }
        $this->_modelMenu = new Model_Tables_Menu();
        $result = $this->_modelMenu->get($menuid);
        $ResourceList_r = $this->_modelMenu->listResource();
        if (is_array($ResourceList_r)&&!empty($ResourceList_r))
        {
            foreach ($ResourceList_r as $i)
            {
                $ResourceList[$i['id']] = $i['resourcename'];
            }
        }
        if (!empty($result['parentid']))
        {
            $parent = $this->_modelMenu->get($result['parentid']);
            $parentid = $parent['parentid'];
            $parentname = $parent['name'];
        }
        $roleids = $result['roleids'];
        $result['roleids'] = explode(',', $result['roleids']);
        //模版赋值
        $form_name = array(
                'name'=>'info[name]',
                'id'=>'info_name',
                'size'=>20,
                'maxlength'=>20,
                'minlength'=>2,
                'value'=>$result['name']
        );
        $form_url = array(
                'name'=>'info[url]',
                'maxlength'=>100,
                'ext'=>'style="width:80%"',
                'value'=>$result['url']
        );
        $form_target = array(
                'options'=>array(
                    '_self'=>'当前窗口',
                    '_blank'=>'新窗口',
                    'top'=>'上窗口',
                    'left'=>'左窗口',
                    'right'=>'右窗口'
                ),
                'name'=>'info[target]',
                'id'=>'target',
                'size'=>1,
                'value'=>$result['target']
        );
        $form_style = array(
                'name'=>'info[style]',
                'size'=>15,
                'value'=>$result['style']
        );
        $form_js = array(
                'name'=>'info[js]',
                'maxlength'=>100,
                'ext'=>'style="width:80%"',
                'value'=>$result['js']
        );
        $form_roleids = array(
                'options'=>$RoleName,
                'name'=>'roleids',
                'id'=>'roleid',
                'width'=>100,
                'value'=>$roleids
        );
        $form_resource = array(
                'options'=>$ResourceList,
                'name'=>'resourceid',
                'id'=>'resourceid',
                'width'=>100,
                'value'=>$result['resourceid']
        );
        $forward = urlencode($_SERVER['HTTP_REFERER']);
        include $this->_dzTemplate->getFile( 'menu.edit.html' );
    }

    public function addAction()
    {
        // action body
        $request = $this->getRequest();
        if ($request->isPost())
        {
            $params = $request->getPost();
//            $filter = new Zend_Filter();
//            $filter->addFilter(new Zend_Filter_StringTrim());
//            $params['info']['name'] = $filter->filter($params['info']['name']);
            if (!Zend_Validate::is($params['info']['name'], 'StringLength', array(2, 20)))
            {
                throw new Exception('菜单名称只能为 2-20 个字符');
            }
            $params['info']['name'] = stripslashes($params['info']['name']);
            $params['info']['url'] = stripslashes($params['info']['url']);
//            $params['info']['css'] = stripslashes($params['info']['css']);
//            $params['info']['js'] = stripslashes($params['info']['js']);

//            if (empty($params['roleids'])||'-99'==$params['roleids'])
//            {
//                $params['info']['roleids'] = '';
//            }
//            else
//            {
//                $params['info']['roleids'] = implode(',', $params['roleids']);
//            }
			$params['info']['keyid'] = '';
            if (empty($params['resourceid']))
            {
                $params['info']['resourceid'] = '0';
            }
            else
            {
                $params['info']['resourceid'] = $params['resourceid'];
            }

            $this->_modelMenu = new Model_Tables_Menu();
            $menuid = $this->_modelMenu->saveMenu($params['info']);
            $this->_modelMenu->update_isfolder();
            $params['forward'] = urldecode($params['forward']);
            $forward = empty($params['forward']) ? $params['forward'] : '/menu/index/';
            $this->_redirect($forward);
        }
        $parentid = intval($this->_getParam('parentid'));

        //读取角色名单
        $modelRole = new Model_Tables_Role();
        $RoleNameList = $modelRole->getNameList();
        $RoleName = array();
        if (!empty($RoleNameList))
        {
            foreach ($RoleNameList as $item)
            {
                $RoleName[$item['roleid']] = $item['name'];
            }
        }
        $this->_modelMenu = new Model_Tables_Menu();
        $ResourceList_r = $this->_modelMenu->listResource();
        if (is_array($ResourceList_r)&&!empty($ResourceList_r))
        {
        	foreach ($ResourceList_r as $i)
        	{
        		$ResourceList[$i['id']] = $i['resourcename'];
        	}
        }
        if (!empty($parentid))
        {
        	$this->_modelMenu = new Model_Tables_Menu();
            $parent = $this->_modelMenu->get($parentid);
            $parentname = $parent['name'];
        }
        //模版赋值
        $form_name = array(
                'name'=>'info[name]',
                'id'=>'info_name',
                'size'=>20,
                'maxlength'=>20,
                'minlength'=>2
        );
        $form_url = array(
                'name'=>'info[url]',
                'maxlength'=>100,
                'ext'=>'style="width:80%"'
        );
        $form_target = array(
                'options'=>array(
                    '_self'=>'当前窗口',
                    '_blank'=>'新窗口',
                    'top'=>'上窗口',
                    'left'=>'左窗口',
                    'right'=>'右窗口'
                ),
                'name'=>'info[target]',
                'id'=>'target',
                'size'=>1
        );
        $form_style = array(
                'name'=>'info[style]',
                'size'=>15
        );
        $form_js = array(
                'name'=>'info[js]',
                'maxlength'=>100,
                'ext'=>'style="width:80%"'
        );
        $form_roleids = array(
                'options'=>$RoleName,
                'name'=>'roleids',
                'id'=>'roleid',
                'width'=>100,
                'value'=>''
        );
        $form_resource = array(
                'options'=>$ResourceList,
                'name'=>'resourceid',
                'id'=>'resourceid',
                'width'=>100,
                'value'=>''
        );
        $forward = urlencode($_SERVER['HTTP_REFERER']);
        include $this->_dzTemplate->getFile( 'menu.add.html' );
    }

}
